﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.XetTotNghiep
{
    public partial class FormQuanLyChungChiDieuKien : QldtSdh.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        public FormQuanLyChungChiDieuKien()
        {
            InitializeComponent();
            gridDataHocVien.AddRowIndexing();
            InitGridLop();
            InitGridHocVien();
            LoadKhoaHoc();
        }

        void InitGridLop()
        {
            gridDataLop.AutoGenerateColumns = false;
            colTenLop.DataPropertyName = "TenLopKhoaNganh";
            colTenCoSoDaoTao.DataPropertyName = "TenCoSoDaoTao";
        }

        void InitGridHocVien()
        {
            gridDataHocVien.AutoGenerateColumns = false;
            colMaHocVien.DataPropertyName = "MaHocVien";
            colHoDem.DataPropertyName = "HoDem";
            colTen.DataPropertyName = "Ten";
            colGioiTinh.DataPropertyName = "GioiTinhStr";
            colNgaySinh.DataPropertyName = "NgaySinh";
            colChungChi.DataPropertyName = "CoChungChi";
            colLoaiChungChi.DataPropertyName = "LoaiChungChi";
            colNgayCapChungChi.DataPropertyName = "NgayCapChungChi";
            colNgoaiNgu.DataPropertyName = "NgoaiNgu";
            colGhiChu.DataPropertyName = "GhiChu";
            colNoiCap.DataPropertyName = "NoiCapChungChi";
            gridDataHocVien.Columns[colNgayCapChungChi.Index].DefaultCellStyle.Format = "dd/MM/yyyy";
        }

        void LoadKhoaHoc()
        {
            allowUpdate = false;
            cbxKhoaHoc.DataSource = BizKhoaHoc.GetList();
            cbxKhoaHoc.DisplayMember = "TenKhoaHoc";
            cbxKhoaHoc.ValueMember = "MaKhoaHocGUID";
            allowUpdate = true;
            LoadNganh();
        }

        void LoadNganh()
        {
            allowUpdate = false;
            var khoaHoc = cbxKhoaHoc.SelectedItem as KhoaHoc;
            if (khoaHoc != null)
            {
                var ls = BizKhoaHoc_NganhDaoTao.GetListByKhoaHoc(khoaHoc.MaKhoaHocGUID);
                cbxNganh.DataSource = ls;
                cbxNganh.DisplayMember = "TenNganhDaoTao";
                cbxNganh.ValueMember = "MaNganhDaoTaoGUID";
            }
            else
            {
                cbxNganh.Items.Clear();
            }
            allowUpdate = true;
            LoadLop();
        }

        void LoadLop()
        {
            allowUpdate = false;
            var khoaHoc = cbxKhoaHoc.SelectedItem as KhoaHoc;
            var nganh = cbxNganh.SelectedItem as KhoaHoc_NganhDaoTao;
            if (khoaHoc != null && nganh != null)
            {
                var ls = BizLopKhoaNganh.GetList(khoaHoc.MaKhoaHocGUID, nganh.MaNganhDaoTaoGUID);
                bdLop.DataSource = ls;
                gridDataLop.DataSource = bdLop;
            }
            else
            {
                bdLop.DataSource = null;
                gridDataLop.DataSource = bdLop;
            }
            allowUpdate = true;
            LoadHocVien();
        }

        void LoadHocVien()
        {
            allowUpdate = false;
            var lop = bdLop.Current as LopKhoaNganh;
            if (lop != null)
            {
                var ls = BizHocVien.GetList(lop.MaLopKhoaNganhGUID);
                bdHocVien.DataSource = ls;
                gridDataHocVien.DataSource = bdHocVien;
                if (ls != null && ls.Count > 0)
                {
                    btnCapNhatChungChi.Enabled = true;
                }
                else btnCapNhatChungChi.Enabled = false;
            }
            else
            {
                btnCapNhatChungChi.Enabled = false;
                bdHocVien.DataSource = null;
                gridDataHocVien.DataSource = bdHocVien;
            }
            allowUpdate = true;
        }

        private void cbxKhoaHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadNganh();
            }
        }

        private void cbxNganh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadLop();
            }
        }

        private void bdLop_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadHocVien();
            }
        }

        private void gridDataHocVien_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.ColumnIndex == colChungChi.Index)
            //{
            //    var hocVien = bdHocVien.Current as HocVien;
            //    HocVien temp = new HocVien { MaHocVien = hocVien.MaHocVien, ChungChi = !hocVien.ChungChi, GhiChu = hocVien.GhiChu };
            //    if (hocVien != null)
            //    {
            //        var rs = BizHocVien.UpdateChungChi(temp);
            //        if (rs != CODE_RESULT_RETURN.ThanhCong)
            //            DialogUtils.ShowError("Không thể cập nhật");
            //    }
            //}
        }

        private void gridDataHocVien_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colGhiChu.Index)
            {
                var hocVien = bdHocVien.Current as HocVien;
                if (hocVien != null)
                {
                    var rs = BizHocVien.UpdateChungChi(hocVien);
                    if (rs != CODE_RESULT_RETURN.ThanhCong)
                        DialogUtils.ShowError("Không thể cập nhật");
                }
            }
        }

        private void btnCapNhatChungChi_Click(object sender, EventArgs e)
        {
            var hocVien = bdHocVien.Current as HocVien;
            if (hocVien != null)
            {
                var f = new FormQuanLyChungChiDieuKien_BoSungHocVien(hocVien);
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    hocVien = BizHocVien.GetByID(hocVien.MaHocVien);
                    gridDataHocVien.Refresh();
                }
            }
        }
    }
}
